package com.mlh.util;

import org.springframework.beans.BeanUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 缪林辉
 * @date 2024/3/12 16:53
 * @DESCRIPTION
 */
public class BeanCopyUtil {
    //构造器私有化，防止构造对象
    private BeanCopyUtil(){}

    //将源对象的某些属性赋值给目标对象
    public static <V>V copyBean(Object target,Class<V> clazz){
        V v = null;
        try {
            v = clazz.newInstance();
            BeanUtils.copyProperties(target,v);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return v;
    }

    //将源对象集合转变成目标对象集合
    public static <O,V>List<V>  copyBeanList(List<O>list,Class<V> clazz){
        //Java 8+ 的流式处理（Stream API)
        return list.stream()
                .map(o -> copyBean(o, clazz))
                .collect(Collectors.toList());
    }

}
